﻿using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore;
using WebApplication1.Infrastructure.DatabaseContext;
using WebApplication1.Core.Domain.Entities;
using WebApplication1.Core.Domain.RepositoryContracts;

namespace WebApplication1.Infrastructure.Repositories {
    public class XiangxixinxiRepository : IXiangxixinxiRepository {

        private readonly SQLiteDbContext _db;

        public XiangxixinxiRepository(SQLiteDbContext dbContext) {
            _db = dbContext;
        }

        public async Task<Xiangxixinxi> AddXiangxixinxi(Xiangxixinxi Xiangxixinxi) {
            await _db.Xiangxixinxis.AddAsync(Xiangxixinxi);
            await _db.SaveChangesAsync();
            return Xiangxixinxi;
        }

        public async Task<bool> DeleteXiangxixinxi(ushort XiangxixinxiID) {
            _db.Xiangxixinxis.RemoveRange(_db.Xiangxixinxis.Where(temp => temp.Xiangmuid == XiangxixinxiID));
            int rowChanged = await _db.SaveChangesAsync();
            return rowChanged > 0;
        }

        public async Task<IEnumerable<Xiangxixinxi>> GetAllXiangxixinxi() {
            return await _db.Xiangxixinxis.ToListAsync();
        }

        public async Task<IEnumerable<Xiangxixinxi>> GetFilteredXiangxixinxi(Expression<Func<Xiangxixinxi, bool>> expression) {
            return await _db.Xiangxixinxis.Where(expression).ToListAsync();
        }

        public async Task<Xiangxixinxi?> GetXiangxixinxiByXiangxixinxiID(ushort XiangxixinxiID) {
            return await _db.Xiangxixinxis.FirstOrDefaultAsync(temp => temp.Xiangmuid == XiangxixinxiID);
        }

        public async Task<bool> UpdateXiangxixinxi(Xiangxixinxi Xiangxixinxi) {
            Xiangxixinxi? matched = await _db.Xiangxixinxis.FirstOrDefaultAsync(temp => temp.Xiangmuid == Xiangxixinxi.Xiangmuid);
            if(matched == null) { return false; }
            matched.XiangMuMingCheng = Xiangxixinxi.XiangMuMingCheng;
            matched.LiXiangDaiMa = Xiangxixinxi.LiXiangDaiMa;
            matched.KeYanBianZhiDanWei = Xiangxixinxi.KeYanBianZhiDanWei;
            matched.KeYanFei = Xiangxixinxi.KeYanFei;
            matched.KeYanPingGuDanWei = Xiangxixinxi.KeYanPingGuDanWei;
            matched.KeYanPingGuFei = Xiangxixinxi.KeYanPingGuFei;
            matched.XiangMuBianHao = Xiangxixinxi.XiangMuBianHao;
            matched.KeYanPiFuWenHao = Xiangxixinxi.KeYanPiFuWenHao;
            matched.ChuShePiFuWenHao = Xiangxixinxi.ChuShePiFuWenHao;
            matched.SheJiDanWei = Xiangxixinxi.SheJiDanWei;
            matched.SheJiFei = Xiangxixinxi.SheJiFei;
            matched.CeHuiDanWei = Xiangxixinxi.CeHuiDanWei;
            matched.CeHuiFei = Xiangxixinxi.CeHuiFei;
            matched.ZhaoBiaoDaiLi = Xiangxixinxi.ZhaoBiaoDaiLi;
            matched.ZhaoBiaoKongZhiJia = Xiangxixinxi.ZhaoBiaoKongZhiJia;
            matched.ZhongBiaoDanWei = Xiangxixinxi.ZhongBiaoDanWei;
            matched.XiangMuJingLi = Xiangxixinxi.XiangMuJingLi;
            matched.ShiGongFuZeRen = Xiangxixinxi.ShiGongFuZeRen;
            matched.ZhongBiaoJia = Xiangxixinxi.ZhongBiaoJia;
            matched.KaiGongShiJian = Xiangxixinxi.KaiGongShiJian;
            matched.JunGongShiJian = Xiangxixinxi.JunGongShiJian;
            matched.YanShouShiJian = Xiangxixinxi.YanShouShiJian;
            matched.YiZhiFuGongChengKuan = Xiangxixinxi.YiZhiFuGongChengKuan;
            matched.JieSuanShenQingShiJian = Xiangxixinxi.JieSuanShenQingShiJian;
            matched.JieSuanBaoJia = Xiangxixinxi.JieSuanBaoJia;
            matched.JieSuanJia = Xiangxixinxi.JieSuanJia;
            matched.JianLiDanWei = Xiangxixinxi.JianLiDanWei;
            matched.JianLiFei = Xiangxixinxi.JianLiFei;
            matched.YiZhiFuJianLiFei = Xiangxixinxi.YiZhiFuJianLiFei;
            await _db.SaveChangesAsync();
            return true;
        }
    }
}
